package com.zbc.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadUtil {
    private static String uploadUrl = "https://upload001test.oss-cn-beijing.aliyuncs.com";

    public static String uploadFile(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString().replace("-","");
        fileName = uuid + suffix;

        String url = "https://oss-cn-beijing.aliyuncs.com";
        String kid = "LTAI5tDuZZ8LPLgmRKuk8cGM";
        String ksd = "bASxCdJor4lyCOsRcrgotmgtkq2FCA";

        OSS clientOss = new OSSClient(url,kid,ksd);
        clientOss.putObject("upload001test",fileName,file.getInputStream());
        clientOss.shutdown();
        return uploadUrl+'/'+fileName;
    }
}
